import { Injectable } from '@nestjs/common';
// import { ConfigService } from '@nestjs/config';
import { WordCollection } from './word-collection.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { PagingResponse } from 'src/constants/common.constants';
import { WordCollectionListDto } from './dto/list.dto';
import { AddWordCollectionDto } from './dto/add.dto';
// import { PassThrough } from 'stream';
@Injectable()
export class WordCollectionService {
  private access_token: string;
  private expires_in: number;
  private timestamp: number;

  constructor(
    @InjectRepository(WordCollection)
    private readonly appWordCollectionRepository: Repository<WordCollection>,
  ) {}

  async findAll(
    uid: string,
    params: WordCollectionListDto,
  ): Promise<PagingResponse<WordCollection>> {
    console.log('params', params);
    const [items, total] = await this.appWordCollectionRepository.findAndCount({
      skip: (params.pageNum - 1) * params.pageSize, // skip the items for the previous pages
      take: params.pageSize,
      where: {
        // ...params.params,
        userId: uid,
      },
      order: {
        updatedAt: 'DESC',
      },
    });
    const { pageSize, pageNum } = params;
    return {
      records: items,
      total,
      pageSize,
      current: pageNum,
      pages: Math.ceil(total / pageSize),
    };
    // const [items] = await this.AppWordCollectionRepository.findAndCount({});
    // return items;
    // return await this.userRepository
    //   .createQueryBuilder()
    //   .where({ deleted: 0 })
    //   .select(['user_name', 'email'])
    //   .getMany();
    //   .find();
  }

  async findOne(id: number) {
    return await this.appWordCollectionRepository.findOne({
      where: {
        id,
      },
    });
  }

  async create(
    userId: string,
    collection: AddWordCollectionDto,
  ): Promise<WordCollection> {
    const data = this.appWordCollectionRepository.create({
      userId,
      ...collection,
    });
    return await this.appWordCollectionRepository.save(data);
  }

  async remove(ids: string): Promise<void> {
    await this.appWordCollectionRepository.delete(ids);
  }
}
